home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / The Hacks! / Interim Executive Decision / Outline Table Demo / sources / CFileItem.cp < prev    next >
Encoding:
Text File  |  1998-06-21  |  5.4 KB  |  262 lines  |  [TEXT/CWIE]

  1. //============================================================================
  2. // CFileItem.cp        ©1997 Metrowerks Inc. All rights reserved
  3. // Original author: John C. Daub
  4. //============================================================================
  5. // A concrete LOutlineItem for items of type "file"
  6.  
  7.  
  8. #include "CFileItem.h"
  9. #include "SendFinderOpen.h"
  10. #include <LOutlineItem.h>
  11. #include <LOutlineTable.h>
  12. #include <UGAColorRamp.h>
  13. #include "OutlineTablePrefix.h"
  14. #include <Gestalt.h>
  15. #include <LString.h>
  16. #include <TextUtils.h>
  17.  
  18. #include "GetFileIcon.h"
  19.  
  20. extern Handle gOnIcon;
  21. extern Handle gOffIcon;
  22.  
  23. StringPtr gGestaltBitNames[] =
  24. {
  25.     "\pDrag Manager Present",
  26.     "\pDrag Manager Floating Window",
  27.     "\pPPC Drag Lib Present",
  28.     "\pDrag Manager Has Image Support",
  29.     "\pCan Start Drag in Float Window"
  30. };
  31.  
  32. // constructor
  33.  
  34. CFileItem::CFileItem(
  35.     int        nameId,
  36.     OSType    selector,
  37.     int        bit
  38. )
  39. {
  40.     OSErr            err = noErr;
  41.     long            response = 0;
  42.  
  43.     fNameId = nameId;
  44.     fSelector = selector;
  45.     fBit = bit;
  46.  
  47.     err = Gestalt(selector, &response);
  48.  
  49.     if (err == noErr)
  50.     {
  51.         fState = ((response & (1L << bit)) ? true : false);
  52.     }
  53.     else
  54.     {
  55.         fState = true;
  56.     }
  57.  
  58. }
  59.  
  60.  
  61. // dtor
  62.  
  63. CFileItem::~CFileItem()
  64. {
  65. }
  66.  
  67. // this is the routine called to know what to draw within the
  68. // table cell. See the comments in LOutlineItem.cp for more info.
  69.  
  70. void
  71. CFileItem::GetDrawContentsSelf(
  72.     const STableCell&        inCell,
  73.     SOutlineDrawContents&    ioDrawContents)
  74. {
  75.     switch (inCell.col)
  76.     {
  77.         default:
  78.         {
  79.             ioDrawContents.outShowSelection = true;
  80.             ioDrawContents.outHasIcon = true;
  81.             ioDrawContents.outIconSuite = (fState? gOnIcon : gOffIcon);
  82.             ioDrawContents.outTextTraits.style = 0;
  83.                         
  84.             if (fNameId >= 0)
  85.             {
  86.                 LString::CopyPStr(gGestaltBitNames[fNameId], ioDrawContents.outTextString);
  87.             }
  88.             else
  89.             {
  90.                 Str255            tmpName;
  91.                 Str255            tmpNumber;
  92.  
  93.                 LString::FourCharCodeToPStr(fSelector, tmpName);
  94.                 NumToString(fBit, tmpNumber);
  95.                 LString::AppendPStr(tmpName, tmpNumber);
  96.                 LString::CopyPStr(tmpName, ioDrawContents.outTextString);
  97.             }
  98.         
  99.             break;
  100.         }
  101.     }
  102. }
  103.  
  104. // just to be cute, we'll draw an adornment (again, see the LOutlineItem.cp
  105. // comments for more information). We'll draw a groovy gray background
  106.  
  107. void
  108. CFileItem::DrawRowAdornments(
  109.     const Rect&        inLocalRowRect )
  110. {
  111.     ShadeRow(UGAColorRamp::GetColor(2), inLocalRowRect);
  112. }
  113.  
  114.  
  115. // just to be cute, when a double-click occurs, we'll send the Finder
  116. // and open document AppleEvent to open our selected item... just to do
  117. // something.
  118.  
  119. void
  120. CFileItem::DoubleClick(
  121.     const STableCell&            /* inCell */,
  122.     const SMouseDownEvent&        /* inMouseDown */,
  123.     const SOutlineDrawContents&    /* inDrawContents */,
  124.     Boolean                        /* inHitText */)
  125. {
  126. //    ThrowIfOSErr_(SendFinderOpenAE(&mFileSpec));
  127. }
  128.  
  129. // ---------------------------------------------------------------------------
  130. //        * TrackContentClick                                        [protected]
  131. // ---------------------------------------------------------------------------
  132. //    Called by ClickCell whenever a mouse-down event occurs within
  133. //    the contents (icon or text) of this cell. Selects or deselects this
  134. //    cell as appropriate and tests for dragging or double-clicking.
  135.  
  136. void
  137. CFileItem::TrackContentClick(
  138.     const STableCell&            /*inCell*/,
  139.     const SMouseDownEvent&        inMouseDown,
  140.     const SOutlineDrawContents&    /*inDrawContents*/,
  141.     Boolean                        inHitText)
  142. {
  143.     inHitText = inHitText;
  144.  
  145. #if 000000
  146.     // If item isn't selected, try to select it now.
  147.     
  148.     Boolean wasSelected = mOutlineTable->CellIsSelected(inCell);
  149.     if (!wasSelected) {
  150.         if (!(inMouseDown.macEvent.modifiers & shiftKey))
  151.             mOutlineTable->UnselectAllCells();
  152.         mOutlineTable->SelectCell(inCell);
  153.         mOutlineTable->UpdatePort();
  154.     }
  155. #endif    
  156.     // See if mouse moves. If it does, track a drag from it
  157.     
  158.     if (::StillDown() && ::WaitMouseMoved(inMouseDown.macEvent.where)) {
  159. //        TrackDrag(inCell, inMouseDown, inDrawContents);
  160.         return;
  161.     }
  162.  
  163. #if 000000
  164.     // Mouse didn't move, see if this was a double-click.
  165.     
  166.     if (LPane::GetClickCount() > 1) {
  167.         DoubleClick(inCell, inMouseDown, inDrawContents, inHitText);
  168.         return;
  169.     }
  170.  
  171.     // If none of the above, and shift key was down, deselect this cell.
  172.     
  173.     if (wasSelected && (inMouseDown.macEvent.modifiers & shiftKey)) {
  174.         mOutlineTable->UnselectCell(inCell);
  175.         mOutlineTable->UpdatePort();
  176.         return;
  177.     }
  178.  
  179.     // Simple click and item is still selected, call it a single click.
  180.     
  181.     if (mOutlineTable->CellIsSelected(inCell)) {
  182.         SingleClick(inCell, inMouseDown, inDrawContents, inHitText);
  183.         return;
  184.     }
  185. #endif
  186.  
  187.     SetState((fState ? false : true));
  188.     return;    
  189. }
  190.  
  191. void CFileItem::SetState(bool state)
  192. {
  193.     long            result;
  194.     OSType            err;
  195.     AppleEvent        aeEvent;                    // Apple event to send
  196.     AppleEvent        aeReply;                    // Unused reply event
  197.     AEDesc            descDest;                    // Addressee for event
  198.     OSType            dest = 'SPJ!';
  199.  
  200.     err = Gestalt(fSelector, &result);
  201.  
  202.     fState = state;
  203.  
  204.     if (fState)
  205.     {
  206.         result |= (1L << ((long)fBit));
  207.     }
  208.     else
  209.     {
  210.         result &= ~(1L << ((long)fBit));
  211.     }
  212.  
  213.     err = ::AECreateDesc (typeApplSignature, &dest, sizeof(OSType), &descDest);
  214.  
  215.     err =
  216.         ::AECreateAppleEvent
  217.         (
  218.             'SPJ!',
  219.             'SPJ!',
  220.             &descDest,
  221.             kAutoGenerateReturnID,
  222.             kAnyTransactionID,
  223.             &aeEvent
  224.         );
  225.  
  226.     err =
  227.         ::AEPutParamPtr
  228.         (
  229.             &aeEvent,
  230.             keyDirectObject,
  231.             typeType,
  232.             &fSelector,
  233.             sizeof(OSType)
  234.         );
  235.  
  236.     err =
  237.         ::AEPutParamPtr
  238.         (
  239.             &aeEvent,
  240.             'ARF ',
  241.             typeLongInteger,
  242.             &result,
  243.             sizeof(long)
  244.         );
  245.  
  246.     err =
  247.         ::AESend
  248.         (
  249.             &aeEvent,
  250.             &aeReply,
  251.             kAEWaitReply + kAECanInteract + kAECanSwitchLayer,
  252.             kAENormalPriority,
  253.             kAEDefaultTimeout,
  254.             nil,
  255.             nil
  256.         );
  257.  
  258.     mOutlineTable->Refresh();
  259.  
  260.     return;
  261. }
  262.